package dao

import (
	"chat/inits"
	"chat/model"
)

func CreateMessage(fromUserId int, toUserId int, message string) bool {
	var min1 = fromUserId
	var max1 = toUserId
	if fromUserId > toUserId {
		min1 = toUserId
		max1 = fromUserId
	}
	chat := &model.Message{
		FromUserId: fromUserId,
		ToUserId:   toUserId,
		Message:    message,
		RoomId:     min1*100000 + max1,
	}
	if inits.DB.Create(chat).Error != nil {
		return false
	}
	return true

}
func ListMessage() (chat []*model.Message, err error) {
	if err = inits.DB.Find(&chat).Error; err != nil {
		return nil, err
	}
	return chat, nil
}
